//
//  BottomNavigationView.swift
//  letusmeet_ios
//
//  Created by Rovo Dev on 2025/1/27.
//

import SwiftUI

struct BottomNavigationView: View {
    @Binding var selectedTab: Int
    
    private let navItems = [
        NavItem(icon: "house", title: "首页", index: 0),
        NavItem(icon: "magnifyingglass", title: "搜索", index: 1),
        NavItem(icon: "plus", title: "发布", index: 2),
        NavItem(icon: "person", title: "我的", index: 3)
    ]
    
    var body: some View {
        HStack(spacing: 0) {
            ForEach(navItems, id: \.id) { item in
                NavItemView(
                    item: item,
                    isSelected: selectedTab == item.index
                ) {
                    withAnimation(.easeInOut(duration: 0.2)) {
                        selectedTab = item.index
                    }
                    handleNavigation(to: item.index)
                }
            }
        }
        .padding(.horizontal, 8)
        .padding(.top, 8)
        .padding(.bottom, 34)
        .background(
            Color(NSColor.controlBackgroundColor)
                .shadow(color: .black.opacity(0.1), radius: 8, x: 0, y: -2)
        )
    }
    
    private func handleNavigation(to index: Int) {
        switch index {
        case 0:
            print("导航到首页")
        case 1:
            print("导航到搜索")
        case 2:
            print("导航到发布")
        case 3:
            print("当前在个人中心")
        default:
            break
        }
    }
}

struct NavItem {
    let id = UUID()
    let icon: String
    let title: String
    let index: Int
}

struct NavItemView: View {
    let item: NavItem
    let isSelected: Bool
    let onTap: () -> Void
    
    var body: some View {
        Button(action: onTap) {
            VStack(spacing: 4) {
                Image(systemName: item.icon)
                    .font(.system(size: isSelected ? 20 : 16))
                    .scaleEffect(isSelected ? 1.1 : 1.0)
                
                Text(item.title)
                    .font(.system(size: 12, weight: isSelected ? .semibold : .medium))
                    .foregroundColor(isSelected ? .orange : .secondary)
            }
            .frame(maxWidth: .infinity)
            .padding(.vertical, 8)
        }
        .buttonStyle(PlainButtonStyle())
        .animation(.easeInOut(duration: 0.2), value: isSelected)
    }
}

#Preview {
    @State var selectedTab = 3
    
    VStack {
        Spacer()
        BottomNavigationView(selectedTab: $selectedTab)
    }
    .background(Color(NSColor.controlBackgroundColor))
}